import request from '@/utils/request'

export function getDictPageList(params) {
    return request({
      url: '/datasourceConf/datasourcePageList',
      method: 'get',
      params
    })
  }

// 新增
export function addDataSource(data) {
  return request({
    url: '/datasourceConf/',
    method: 'post',
    data
  })
}

//修改
export function updateDataSource(data) {
  return request({
    url: '/datasourceConf/',
    method: 'put',
    data
  })
}

//删除数据源
export function deleteSource(id) {
  return request({
    url: `/datasourceConf/${id}`,
    method: 'delete',
  })
}

export function getList(params) {
  return request({
    url: '/datasourceConf/getList',
    method: 'get',
    params
  })
}

//获取表的信息
export function getTablePageList(params) {
  return request({
    url: '/generator/page',
    method: 'get',
    params
  })
}

//获取表的信息
export function getTableInfoPageList(params) {
  return request({
    url: '/generator/table',
    method: 'get',
    params
  })
}
//预览
export function getPreview(params) {
  return request({
    url: '/generator/preview',
    method: 'get',
    params
  })
}

//下载
export function handleDown(table) {
  return request({
    url: '/generator/code',
    method: 'post',
    data: table,
    responseType: 'arraybuffer'
  }).then((response) => { // 处理返回的文件流
    const blob = new Blob([response.data], {type: 'application/zip'})
    const filename = table.tableName + '.zip'
    const link = document.createElement('a')
    link.href = URL.createObjectURL(blob)
    link.download = filename
    document.body.appendChild(link)
    link.click()
    window.setTimeout(function () {
      URL.revokeObjectURL(blob)
      document.body.removeChild(link)
    }, 0)
  })
}